home *** CD-ROM | disk | FTP | other *** search
/ Power Hacker 2003 / Power_Hacker_2003.iso / Exploit and vulnerability / s0ftpj / N0Sp00f.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-12-17  |  7.3 KB  |  279 lines

  1. /*
  2.  * N0Sp00f.c            Semplice modulo per evitare che qualche lama
  3.  *                decida di usare il nostro sistema come hop di
  4.  *                lancio per pacchetti IP spoofati. Intercetta
  5.  *                la chiamata di sistema socketcall() per il
  6.  *                parametro IP_HDRINCL passato a setsockopt(). 
  7.  *                Purtroppo Linux ha le seguenti istruzioni in
  8.  *                ~/net/ipv4/af_inet.c:
  9.  *
  10.  *                case SOCK_RAW:
  11.  *                           if (protocol == IPPROTO_RAW)
  12.  *                                   sk->ip_hdrincl = 1;
  13.  *
  14.  *                che permettono di bypassare tranquillamente
  15.  *                l'uso di setsockopt() ...
  16.  *                In questo caso dovremo anche controllare in
  17.  *                 ogni sys_sendto() per evitare lo spoof degli
  18.  *                IP sorgente dalla nostra box. Comunque, ogni
  19.  *                tentativo verra' loggato. Una comoda password
  20.  *                da inserire in un file in /proc/net/ potra'
  21.  *                servire al 'legittimo' root per operare sugli
  22.  *                header dei pacchetti.
  23.  *
  24.  *                Implementazione per Linux 2.2.x
  25.  *
  26.  *                                           __NO__(C)2000 FuSyS [S0ftPj|BFi]
  27.  *                               <fusys@s0ftpj.org>
  28.  *
  29.  * Compilate con:           gcc -c -O2 -fomit-frame-pointer N0Sp00f.c
  30.  * Installate con:          insmod N0Sp00f.o <DEVICE=interfaccia>
  31.  *
  32.  * Credits:            LKMPG per /proc, pIGpEN per avermi spronato,
  33.  *                i LAMAH di tutto il mondo per i DoS [se privi
  34.  *                di ogni significato 'antagonista' ...],
  35.  *                Gigi_Sull per tutti gli Aieeeee' =)
  36.  *
  37.  */
  38.  
  39. #define MODULE
  40. #define __KERNEL__
  41. #define CONFIG_PROC_FS
  42. #include <linux/module.h>
  43.  
  44. #include <linux/types.h>
  45. #include <linux/stat.h>
  46. #include <linux/fcntl.h>
  47. #include <linux/proc_fs.h>
  48. #include <linux/mm.h>
  49. #include <linux/if.h>
  50. #include <linux/ip.h>
  51. #include <linux/notifier.h>
  52. #include <linux/inetdevice.h>
  53. #include <linux/netdevice.h>
  54. #include <sys/syscall.h>
  55. #include <asm/uaccess.h>
  56. #include <asm/unistd.h>
  57.  
  58. #define PASS_LENGTH     50
  59. #define PASSWORD    "[S0ftPj|BFi]"
  60. #define LKMNAME         "N0Sp00f"
  61. #define LOG
  62.  
  63. int (*old_socketcall) (int, unsigned long *);
  64. int (*old_query_module)(const char *, int, char *, size_t, size_t *) ;
  65. extern void *sys_call_table[];
  66. static char password[PASS_LENGTH];
  67. char *DEVICE="eth0";
  68. char Rip[15];
  69. int errno;
  70.  
  71. MODULE_PARM(DEVICE, "s");
  72.  
  73. char *ntoa(unsigned long ip) {
  74.         static char buff[18];
  75.         char *p;
  76.         p = (char *) &ip;
  77.         sprintf(buff, "%d.%d.%d.%d",
  78.                 (p[0] & 255), (p[1] & 255), (p[2] & 255), (p[3] & 255));
  79.         return(buff);
  80. }
  81.  
  82. void getIPs() 
  83. {
  84.     struct device *dev;
  85.     struct in_device *in_dev;
  86.     struct in_ifaddr **ifap = NULL;
  87.     struct in_ifaddr *ifa = NULL;
  88.     
  89.     dev =(struct device *)(dev_get(DEVICE));
  90.     in_dev = dev->ip_ptr;
  91.            if ((in_dev=dev->ip_ptr) != NULL) {
  92.                    for (ifap=&in_dev->ifa_list; (ifa=*ifap) != NULL; ifap=&ifa->ifa_next)
  93.                            if (strcmp(DEVICE, ifa->ifa_label) == 0)
  94.                                    break;
  95.            }
  96.     strncpy(Rip, ntoa(ifa->ifa_local), 15);
  97. }
  98.  
  99. static ssize_t module_output(struct file *file, char *buf, size_t len, loff_t *offset)
  100. {
  101.   static int finished = 0;
  102.   int i;
  103.   char message[PASS_LENGTH+30];
  104.  
  105.   if (finished) {
  106.     finished = 0;
  107.     return 0;
  108.   }
  109.   sprintf(message, "N0SP00F Password\n");
  110.   for(i=0; i<len && message[i]; i++)
  111.     put_user(message[i], buf+i);
  112.   finished = 1;
  113.   return i;
  114. }
  115.  
  116. static ssize_t module_input(struct file *file, const char *buf, size_t length, loff_t *offset)
  117. {
  118.   int i;
  119.  
  120.   for(i=0; i<PASS_LENGTH-1 && i<length; i++)
  121.     get_user(password[i], buf+i);
  122.   password[i] = '\0';
  123.   return i;
  124. }
  125.  
  126. static int module_permission(struct inode *inode, int op)
  127. {
  128.   if (current->euid == 0)
  129.     return 0;
  130.   return -EACCES;
  131. }
  132.  
  133. int module_open(struct inode *inode, struct file *file)
  134. {
  135.   MOD_INC_USE_COUNT;
  136.   return 0;
  137. }
  138.  
  139. int module_close(struct inode *inode, struct file *file)
  140. {
  141.   MOD_DEC_USE_COUNT;
  142.   return 0;
  143. }
  144.  
  145. static struct file_operations N0SP00F_fops =
  146.   {
  147.     NULL, module_output, module_input, NULL, NULL, NULL,
  148.     NULL, module_open, NULL, module_close,
  149.   };
  150.  
  151. static struct inode_operations N0SP00F_iops =
  152.   {
  153.     &N0SP00F_fops, NULL, NULL, NULL, NULL,
  154.     NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  155.     NULL, NULL, NULL, module_permission
  156.   };
  157.  
  158. static struct proc_dir_entry N0SP00F =
  159.   {
  160.     0, 7, "N0SP00F", S_IFREG | S_IRUGO | S_IWUSR,
  161.     1, 0, 0, 50, &N0SP00F_iops, NULL
  162.   };
  163.  
  164. int new_socketcall(int call, unsigned long *args)
  165. {
  166.     int socket;
  167.         unsigned long *sargs = args;
  168.     unsigned long a0, a1, a2;
  169.     void *buf;
  170.     struct iphdr *ip;
  171.  
  172.     if(call == SYS_SETSOCKOPT) {
  173.        if(sargs[2] == IP_HDRINCL) {
  174.         if(!strstr(password, PASSWORD)) {
  175.                 printk(KERN_INFO 
  176.                 "<N0Sp00f> IP_HDRINCL: %s with UID:%d and TTY:%s\n",
  177.                 current->comm, current->uid,
  178.                 current->tty->driver.driver_name);
  179.             return -EPERM;
  180.         }
  181.        }
  182.        return socket = (*old_socketcall) (call, args);
  183.     }
  184.     else if(call == SYS_SENDTO) {
  185.         get_user(a0, sargs);
  186.                 get_user(a1, sargs + 1);
  187.         get_user(a2, sargs + 2);
  188.         buf = (void*)kmalloc(a2, GFP_KERNEL);
  189.         copy_from_user(buf, (void *) a1, a2);
  190.         ip = (struct iphdr *)(void *)buf ;
  191.         if(ip->ihl == 5 && ip->version == 4) {
  192.            if(!strstr(password, PASSWORD)) {
  193.             if(!strstr(Rip, (ntoa(ip->saddr)))) {
  194.             #ifdef LOG
  195.                 printk(KERN_INFO
  196.                 "<N0Sp00f> sys_sendto\(): %s with UID:%d, TTY:%s and IP: %s\n",
  197.                 current->comm, current->uid, 
  198.                 current->tty->driver.driver_name, ntoa(ip->saddr));
  199.             #else
  200.                 printk(KERN_INFO
  201.                 "<N0Sp00f> sys_sendto\(): %s with UID:%d, TTY:%s\n",
  202.                 current->comm, current->uid,current->tty->driver.driver_name);            
  203.             #endif
  204.                 return -EPERM;
  205.             }
  206.            }
  207.         }
  208.     }
  209.     return socket = (*old_socketcall) (call, args);
  210. }
  211.  
  212. int new_query_module(const char *name, int which, char *buf, size_t bufsize,
  213.         size_t *ret)
  214. {
  215.         int res;
  216.         int cnt;
  217.         char *ptr, *match;
  218.  
  219.         res = (*old_query_module)(name, which, buf, bufsize, ret);
  220.  
  221.         if(res == -1)
  222.                 return(-errno);
  223.  
  224.         if(which != QM_MODULES)
  225.                 return(res);
  226.  
  227.         ptr = buf;
  228.  
  229.         for(cnt = 0; cnt < *ret; cnt++) {
  230.                 if(!strcmp(LKMNAME, ptr)) {
  231.                         match = ptr;
  232.                         while(*ptr)
  233.                                 ptr++;
  234.                         ptr++;
  235.                         memcpy(match, ptr, bufsize - (ptr - (char *)buf));
  236.                         (*ret)--;
  237.                         return(res);
  238.                 }
  239.                 while(*ptr)
  240.                         ptr++;
  241.                 ptr++;
  242.         }
  243.  
  244.         return(res);
  245. }
  246.  
  247. void ttycredit(char *str)
  248. {
  249.     struct tty_struct *mytty;
  250.  
  251.     if((mytty = current->tty) != NULL) {
  252.         (*(mytty->driver).write)(mytty, 0, str, strlen(str));
  253.     }
  254. }
  255.     
  256. int init_module(void)
  257. {
  258.         EXPORT_NO_SYMBOLS;
  259.  
  260.     getIPs();
  261.     old_socketcall = sys_call_table[SYS_socketcall];
  262.     sys_call_table[SYS_socketcall] = (void *) new_socketcall;
  263.         old_query_module = sys_call_table[SYS_query_module];
  264.         sys_call_table[SYS_query_module]=(void *)new_query_module;
  265.     ttycredit("\n\033[1;34m---[ \033[1;32mN0Sp00f\033[1;34m");
  266.     ttycredit(" Linux 2.2.x LKM by FuSyS [S0ftPj|BFi] ]---\033[0m\r\n\r\n");
  267.     printk(KERN_INFO "Loading N0Sp00f to protect bypassing %s\n", Rip);
  268.     return proc_register(proc_net, &N0SP00F);
  269. }
  270.  
  271. void cleanup_module(void)
  272. {
  273.     proc_unregister(proc_net, N0SP00F.low_ino);
  274.     sys_call_table[SYS_socketcall] = old_socketcall;
  275.         sys_call_table[SYS_query_module] = old_query_module;
  276.     ttycredit("\n\033[1;34m Modulo N0Sp00f Disattivato\033[0m\r\n\r\n");
  277.     printk(KERN_INFO "Modulo N0Sp00f Disattivato\n");
  278. }
  279.